import datetime

from dao.comment_store import Comment_store
from dao.user_store import UserStore
from utils.date import get_date_now
import time


class CommentService():
    def getComment(self, blog_id):
        comments = Comment_store().getCommentByBlogId(blog_id)
        # 使用列表推导式提取user_number
        user_number = [comment['user_number'] for comment in comments if 'user_number' in comment]
        # 去除重复项
        user_number = list(set(user_number))
        user_dict = []
        for number in user_number:
            userInfo = UserStore().queryByNumber(number)
            user_dict.append(userInfo[0])
        for comment in comments:
            number = comment["user_number"]
            # 查找user_dict中用户编号为number的字典数据
            for user in user_dict:
                if user and user['number'] == number:
                    # 查找到用户后，添加username到评论中
                    comment['username'] = user.get('username')
                    break  # 找到后退出循环

        return comments

    def countByBlogId(self, blog_id):
        return Comment_store().countByBlogId(blog_id)

    def addComment(self, comment):
        comment.created_time = datetime.datetime.now()
        return Comment_store().addComment(comment)

    def deleteComment(self,id):
        return Comment_store().deleteCommentById(id)